package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.PlanInfo;
import com.test.dao.PlanInfoDao;
import com.test.service.PlanInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工计划(PlanInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("planInfoService")
public class PlanInfoServiceImpl implements PlanInfoService {
    @Resource
    private PlanInfoDao planInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PlanInfo queryById(Integer id) {
        return this.planInfoDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param planInfo 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public ResData queryByPage(PlanInfo planInfo, PageRequest pageRequest) {
        int total = Integer.parseInt(String.valueOf(this.planInfoDao.count(planInfo)));
        List<PlanInfo> planInfoList = planInfoDao.queryAllByLimit(planInfo, pageRequest);
        int pageSize = pageRequest.getPageSize();
        int page;
        page=total%pageSize==0? total/pageSize:total/pageSize+1;
        List<MyObject> objectList = planInfoList.stream().map(planInfo1 -> new MyObject(planInfo1)).collect(Collectors.toList());
        return new ResData(ResCode.SUCCESS,objectList,total,page);
    }

    /**
     * 新增数据
     *
     * @param planInfo 实例对象
     * @return 实例对象
     */
    @Override
    public PlanInfo insert(PlanInfo planInfo) {
        this.planInfoDao.insert(planInfo);
        return planInfo;
    }

    /**
     * 修改数据
     *
     * @param planInfo 实例对象
     * @return 实例对象
     */
    @Override
    public PlanInfo update(PlanInfo planInfo) {
        this.planInfoDao.update(planInfo);
        return this.queryById(planInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.planInfoDao.deleteById(id) > 0;
    }
}
